import request from '@/utils/request'
import { setToken,removeToken } from '@/utils/storage'
import {LOGIN,LOGOUT} from '../actionTypes'

export const userLogin=(payload)=>{
    return async (dispatch)=>{
        const res=await request({
             url: '/authorizations',
             method:'post',
             data: payload
        })
        console.log('登录请求结果',res);
        const token=res.data.token
        // 存储到本地
        setToken(token)
        // redux 中存储
        dispatch({
            type:LOGIN,
            payload:token
        })
    }
}

export const logout =()=>{
    return dispatch=>{
        // 清空本地 token
        removeToken()
        // 清空 redux
        dispatch({
            type:LOGOUT
        })
    }
}
